package cn.itcast.config;

import com.github.wxpay.sdk.Config;

import com.github.wxpay.sdk.WXPayRequest;
import com.github.wxpay.sdk.WXPayUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by leiylong on 2020/6/4
 */
public  class WIPayTest {

    public static void main(String[] args) {
        Config config=new Config();
        try {
            //1.封装请求参数
            Map<String,String> map=new HashMap();
            map.put("appid",config.getAppID());//公众账号ID
            map.put("mch_id",config.getMchID());//商户号
            map.put("nonce_str", WXPayUtil.generateNonceStr());//随机字符串
            map.put("body","青橙");//商品描述
            map.put("out_trade_no","51552");//订单号
            map.put("total_fee","1");//金额
            map.put("spbill_create_ip","127.0.0.1");//终端IP
            //通知地址
            map.put("notify_url","https://www.baidu.com/");//回调地址
            map.put("trade_type","NATIVE");//交易类型
            String xmlParam  = WXPayUtil.generateSignedXml(map, config.getKey());
            System.out.println("参数："+xmlParam);

            //2.发送请求
            WXPayRequest wxPayRequest=new WXPayRequest(config);
            String xmlResult = wxPayRequest.requestWithCert("/pay/unifiedorder", null, xmlParam, false);
            System.out.println("结果："+xmlResult);

            //3.解析返回结果
            Map<String, String> mapResult = WXPayUtil.xmlToMap(xmlResult);
            String code_url=mapResult.get("code_url");

            System.out.println(code_url);

        } catch (Exception e) {
            e.printStackTrace();

        }

    }
}
